<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\UpdateStatus;
use App\Admin\Repositories\Posting;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\JsonResponse;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use GuzzleHttp\Client;


class PostingController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Posting(), function (Grid $grid) {
            $grid->column('id')
                ->sortable();
            $grid->column('title')
                ->setAttributes(['style' =>
                    'max-width: 250px;
                    overflow: hidden;
                    white-space: nowrap;
                    text-overflow: ellipsis;']);
            $grid->column('context')
                ->setAttributes(['style' =>
                    'max-width: 250px;
                    overflow: hidden;
                    white-space: nowrap;
                    text-overflow: ellipsis;']);
            $grid->actions([new UpdateStatus()]);
            $grid->column('user_id');
            $grid->column('share_times');
            $grid->column('comments_times');
//            $grid->column('url');
            $grid->column('create_time');
            $grid->column('audit_type')
                ->bool([1=>true,2=>false]);
            $grid->column('source');
            $grid->column('audit_type')
                ->using([1 => '审核通过', 0 => '审核中',2=>'未通过'])
                ->dot([ 1 => 'primary', 2 => 'danger',3=>'success']);
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
            $grid->setActionClass(Grid\Displayers\Actions::class);
            $grid->disableEditButton();
            $grid->disableQuickEditButton();
            $grid->disableViewButton();
            $grid->disableCreateButton();

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Posting(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('context');
            $show->field('user_id');
            $show->field('audit_type');
            $show->field('share_times');
            $show->field('comments_times');
            $show->field('create_time');
            $show->field('url');
            $show->field('source');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {

        return Form::make(new Posting(), function (Form $form) {
            $form->text('id');
            $form->display('title');
            $form->display('context');
            $form->text('url','图片');
            if (!$form->isEditing()) {
                $form->text('user_id');
                $form->text('share_times');
                $form->text('comments_times');
                $form->datetime('create_time');
                $form->text('source');
            }
            $form->switch('audit_type', '发布？');

            // 去除整个工具栏内容
            $form->disableHeader();
            // 添加开关操作
        });
    }






}
